//
//  ShopCartToolbar.m
//  EasyToTesco
//
//  Created by mc on 16/2/18.
//  Copyright © 2016年 Jack. All rights reserved.
//

/**
 *  工具条包括两个button和一个label:
 *  1. 点击全选按钮时, 全选按钮图片改变, (传入items空数组)表格刷新,
 *  所有的cell都取消选中状态, 按钮图片改变, 总金额改变, 结算数量改变;
 *  2. cell上的按钮的选中与否决定着总金额和结算数量
 *  3. 当重新将所有的cell选中时, 全选按钮图片自动改变
 *
 */

#import "ShopCartToolbar.h"


//商品信息头文件

//
#import "TotalPriceView.h"


#define SCToolBarButtonFontSize         [UIFont systemFontOfSize:17]
#define SCToolBarButtonFontSizeSmall    [UIFont systemFontOfSize:14]
#define SCPriceLabelWidth               170




@interface ShopCartToolbar(){
    BOOL buttonClickjudge;
}

@end

@implementation ShopCartToolbar


#pragma mark - 初始化
- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        buttonClickjudge = YES;
        //添加工具视图上的按钮
        [self addSubviews];
    }
    return self;
}



#pragma mark - 添加按钮
- (void)addSubviews {
    // 全选按钮
    
    _selectButton = [self addOneButtonWithTitle:@"全选"
                                      titleFont:SCToolBarButtonFontSizeSmall
                                          image:[UIImage imageNamed:@"gouwucheNormal"]
                                  selectedImage:[UIImage imageNamed:@"gouwucheSelect"]];
    _selectButton.selected = YES;
    
    // 总计金额按钮
    _totalPriceView = [self addOneButtonWithTitle:@"￥0.00"
                                        titleFont:SCToolBarButtonFontSize
                                            image:nil
                                    selectedImage:nil];
    [_totalPriceView setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
//    TotalPriceView *totalView =[[TotalPriceView alloc] init];
//    [self addSubview:totalView];
    
    
    
    // 结算按钮
    [self addPayButton];
}
#pragma mark 分别添加左边两个按钮
- (UIButton *)addOneButtonWithTitle:(NSString *)title
                          titleFont:(UIFont *)font
                              image:(UIImage *)image
                      selectedImage:(UIImage *)selectedImage {
    // 创建
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    
    // 图片
    [button setImage:image forState:UIControlStateNormal];
    [button setImage:selectedImage forState:UIControlStateSelected];
    
    // 背景
//    [button setBackgroundImage:[UIImage imageNamed:@"blackBack"] forState:UIControlStateNormal];
    // 文字标题
    [button setTitle:title forState:UIControlStateNormal];
    [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    button.titleLabel.font = font;
    // 点击时图片不闪烁
    button.adjustsImageWhenHighlighted = NO;
    // 点击事件
    [button addTarget:self action:@selector(clickButton:) forControlEvents:UIControlEventTouchUpInside];
    // 标记
    button.tag = self.subviews.count;
   
    // 添加
    [self addSubview:button];
    
    return button;
}

#pragma mark 结算按钮
- (void)addPayButton {
    // 创建
    UIButton *payButton = [UIButton buttonWithType:UIButtonTypeCustom];
    
    [payButton setTitle:@"去结算" forState:UIControlStateNormal];

    // 背景
//    [payButton setBackgroundImage:[UIImage imageNamed:@"wandershop_red_buy"] forState:UIControlStateNormal];
    [payButton setBackgroundColor:[UIColor colorWithRed:242/255.0 green:75/255.0 blue:72/255.0 alpha:1]];
    // 点击时图片不闪烁
    payButton.adjustsImageWhenHighlighted = NO;

    // 点击事件
    [payButton addTarget:self action:@selector(clickButton:) forControlEvents:UIControlEventTouchUpInside];
    // 标记
    payButton.tag = self.subviews.count;
    // 引用
    _payButton = payButton;
    [self addSubview:payButton];
}

#pragma mark -点击按钮
- (void)clickButton:(UIButton *)button {
    
    if ([_delegate respondsToSelector:@selector(shopCartToolBar:didClickButton:)]) {
        [_delegate shopCartToolBar:self didClickButton:button];
    }
}

#pragma mark - 布局子控件
- (void)layoutSubviews {
    [super layoutSubviews];
    
    // 全选
    CGFloat selectX = 0;
    CGFloat selectY = 0;
    CGFloat selectWidth = (self.width - SCPriceLabelWidth) * 0.5;
    CGFloat selectHeight = self.height;
    _selectButton.frame = CGRectMake(selectX, selectY, selectWidth, selectHeight);
    // 合计
    CGFloat countX = CGRectGetMaxX(_selectButton.frame);
    CGFloat countY = 0;
    CGFloat countWidth = SCPriceLabelWidth;
    CGFloat countHeight = self.height;
    self.totalPriceView.frame = CGRectMake(countX, countY, countWidth, countHeight);
    [self.totalPriceView setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    // 结算
    CGFloat payX = CGRectGetMaxX(self.totalPriceView.frame);
    CGFloat payY = 0;
    CGFloat payWidth = selectWidth;
    CGFloat payHeight = self.height;
    _payButton.frame = CGRectMake(payX, payY, payWidth, payHeight);
}

@end
